package LCP;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @Author: Siantar
 * @Date: 2023-06-21-10:52
 * @Description: 1.0
 */
public class LCP0041 {
    int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};

    public int flipChess(String[] chessboard) {
        int res = 0;
        for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[0].length(); j++) {
                if (chessboard[i].charAt(j) == '.') {
                    res = Math.max(res, dfs(chessboard, i, j));
                }
            }
        }
        return res;
    }

    public int dfs(String[] chessboard, int dx, int dy) {
        int n = chessboard.length, m = chessboard[0].length();
        int[][] board = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                board[i][j] = chessboard[i].charAt(j);
            }
        }
        int res = 0;
        Queue<int[]> queue = new ArrayDeque<>();
        queue.offer(new int[]{dx, dy});
        board[dx][dy] = 'X';
        while (!queue.isEmpty()) {
            int[] t = queue.poll();
            for (int i = 0; i < 8; i++) {
                if (judge(board, t[0], t[1], dir[i][0], dir[i][1])) {
                    int x = t[0] + dir[i][0], y = t[1] + dir[i][1];
                    while (board[x][y] == 'O') {
                        res++;
                        queue.offer(new int[]{x, y});
                        board[x][y] = 'X';
                        x += dir[i][0];
                        y += dir[i][1];
                    }
                }
            }
        }
        return res;
    }

    public boolean judge(int[][] board, int x, int y, int dx, int dy) {
        x += dx;
        y += dy;
        while (x >= 0 && y >= 0 && x < board.length && y < board[0].length) {
            if (board[x][y] == 'X') {
                return true;
            } else if (board[x][y] == '.') {
                return false;
            }
            x += dx;
            y += dy;
        }
        return false;
    }


}
